import {reqGoodsInfo,reqAddOrUpdateShopCart} from "@/api";
//封装游客身份模块uuid->生成一个随机字符串
import {getUUID} from '@/utils/uuid_token'
const state={
   goodsList:{},
    //游客临时身份
    uuid_token:getUUID()
}
const mutations={
    GETGOODSLIST(state,goodsList){
        state.goodsList=goodsList
    },
}
const actions={
    //通过API，向服务器发请求，获取服务器
    async getGoodsList({commit},skuId){
        let result =await reqGoodsInfo(skuId)
        if(result.code==200){
            commit("GETGOODSLIST",result.data)
        }
    },
    //将产品添加到购物车中
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
        //加入购物车返回的信息,前台将参数带给服务器,没有返回数据，所以不用在仓库存储数据
        let result=await reqAddOrUpdateShopCart(skuId,skuNum)
        if (result.code==200){
            return "ok"
        }else {
            return Promise.reject(new Error('fail'))
        }
    }
}
const getters={
    categoryView(state){
        //计算出来的值至少是一个空对象
        return state.goodsList.categoryView||{}
    },
    skuInfo(state){
        return state.goodsList.skuInfo||{}
    },
    //产品售卖信息
    spuSaleAttrList(state){
        return state.goodsList.spuSaleAttrList||[]
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}